#!/bin/bash
#mysql安装
main_path=/usr/local/
install_name=/home/hadoop/software/mysql/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
install_path=/usr/local/mysql-5.7.24-linux-glibc2.12-x86_64
env_fun() #环境
{
echo "-------检测本机环境，请稍等-------"
username=`cat /etc/passwd|grep mysql|cut -c 1-5`
        if [ $username == "mysql" ];then
                echo "用户检测：mysql已存在"
                return 10
        else
                echo "用户检测：mysql用户不存在,添加"
                groupadd mysql
                useradd -r -g mysql -s /bin/false mysql
                return 12
        fi
}
#判断mysql是否安装(rpm、source)
check_fun()
{ # 
rpm_name=`rpm -aq |grep mysql-server`
        if [ ! $rpm_name ];then
                echo "开始检测源码包是否安装,请稍等......"
                        if [ -d $install_path ];then
                                echo "源码:$install_path目录已存在,退出本次安装."
                                exit 0
                        else
                                echo "本机未安装任何mysql程序,将为您开始安装$install_name.请稍等......"
                                install_fun
                        fi

        else
                echo -n "rpm mysql已安装,是否删除?[yes|no]"
                read select
                        if [ $select = yes ];then
                                rpm -ev $rpm_name --nodeps
                                rm -rf /var/log/mysqld.log
                                check_fun
                        else
                                exit 0
                        fi
        fi
}
install_fun() #安装函数
{
cd $main_path
echo "当前目录 $PWD"
echo "正在解压，请稍等......"
tar zxvf $install_name -C $main_path
echo "建立相应目录"
ln -s $install_path mysql
mkdir mysql/data
mkdir mysql/mysql-files
mkdir mysql/log
mkdir mysql/tmp
chown -R mysql:mysql mysql
chown -R mysql:mysql $install_path
chmod 750 mysql/mysql-files

#mkdir -p /var/log/mariadb
#chown -R mysql:mysql  /var/log/mariadb/

install_config

cd mysql
echo 'init mysql ,create user root has no password.....'
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 
bin/mysql_ssl_rsa_setup  --datadir=/usr/local/mysql/data/            

echo 'start mysql......'
cp support-files/mysql.server /etc/init.d/mysql
service mysql start

echo 'create password root for root,allow remote login'
exe_mysql
#配置
}

install_config() #加载配置
{
touch /etc/my.cnf
cat > /etc/my.cnf << EOF
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
socket=/usr/local/mysql/tmp/mysql.sock
log-error=/usr/local/mysql/log/mysqld.log
pid-file=/usr/local/mysql/tmp/mysqld.pid

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[client]
socket=/usr/local/mysql/tmp/mysql.sock
EOF
}

# execute mysql script
exe_mysql()
{
bin/mysql -uroot <<EOF
use mysql;
alter user 'root'@'localhost' identified by 'root';
update  user  set host='%' where user='root';
flush privileges;
EOF
}

main()
{
echo "#***********************************#"
echo "# 欢迎使用mysql安装脚本 #"
echo "# Copyright 2018 by anyeway #"
echo "#***********************************#"
sleep 3
#开始执行，调用检测函数
env_fun
re=$?
if [ re = 10 ];then
        check_fun
else
        check_fun
fi
}
main
exit 0
